package com.dreamwhale.machinery.logistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dreamwhale.machinery.logistics.entity.TransportCompanyInfo;
import com.dreamwhale.machinery.logistics.entity.TransportInfo;
import com.dreamwhale.machinery.logistics.mapper.TransportInfoMapper;
import com.dreamwhale.machinery.logistics.service.TransportInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 托运信息 服务实现类
 * </p>
 *
 * @author Author
 * @since 2020-05-28
 */
@Service
public class TransportInfoServiceImpl extends ServiceImpl<TransportInfoMapper, TransportInfo> implements TransportInfoService {

    @Resource
    private TransportInfoMapper transportInfoMapper;

    @Override
    public int insertTransportInfos(TransportInfo transportInfo) {
        transportInfo.setCreateTime(LocalDateTime.now());
        return transportInfoMapper.insert(transportInfo);
    }

    @Override
    public List<TransportInfo> selectTransportInfoMap(Integer userId) {
        QueryWrapper<TransportInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).eq("del_flag", 2);
        return transportInfoMapper.selectList(queryWrapper);
    }

    @Override
    public List<TransportInfo> selectTransportInfos(Integer infoId) {
        QueryWrapper<TransportInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flag", 2).eq("id", infoId);
        return transportInfoMapper.selectList(queryWrapper);
    }

    @Override
    public TransportInfo selectTransportInfoByTypeId(Integer infoId) {
        return transportInfoMapper.selectTransportInfoByTypeId(infoId);
    }

    @Override
    public List<TransportInfo> selectTransportInfoHome(Integer type, Integer types, Integer brandId, Integer cateId, String title, Integer carTypeId) {
        QueryWrapper<TransportInfo> queryWrapper = new QueryWrapper<>();
        if (type != null) {
            queryWrapper.orderByDesc("count_number");
        }
        if (types != null) {
            queryWrapper.orderByDesc("create_time");
        }
        if (brandId != null) {
            queryWrapper.eq("brand_id", brandId);
        }
        if (cateId != null) {
            queryWrapper.eq("cate_id", cateId);
        }
        if (title != null) {
            queryWrapper.like("title", title);
        }
        if (carTypeId != null) {
            queryWrapper.like("car_type_id", carTypeId);
        }
        return transportInfoMapper.selectList(queryWrapper);
    }
}
